import java.util.Vector;


public class Melchior3 {

	Board board1;
	Tree tree;
	int player;
	int turn;
	int boardSize;
	int numMoves;
	boolean playerSwitch;
	Melchior3(int aBoardSize, int playerNum)
	{
		boardSize = aBoardSize;
		board1 = new Board(aBoardSize);
		tree = new Tree();
		player = playerNum;
		playerSwitch = false;
		if(player == 0)
			turn = 1;
		else
			turn = 2;
		numMoves = 0;
	}
	
	
	public void makeMove(int boardsize, int[][] boardstate, int[] lastmove, Long maxtime, int[] newmove)
	{
		playerSwitch = false;
		//board1.board = boardstate;
		//board1.size = boardsize;
		if(lastmove[0] != -1 && lastmove[1] != -1){
			System.out.println("player is " + player);
			board1.insertPiece(lastmove[0], lastmove[1], -player);
		}
		
		Board tmpBoard = new Board(board1.size);
		tmpBoard.copy(board1);
		/*for(int i = 0; i < board1.size; i++)
			for(int j = 0; j < board1.size; j++)
				tmpBoard.board[i][j] = board1.board[i][j];*/
		
		/*System.out.println("move" + 1);
		tree.printTile(board1.board, board1.size);
		System.out.println("end move");*/
		
		Long s = System.currentTimeMillis();
		Tree.node n = tree.idastar(tmpBoard, 3, player, numMoves, maxtime);
		Long e = System.currentTimeMillis();
		System.out.println("HOW LONG - " + (e-s));
		numMoves++;
		
		System.out.println("move b: " + n.board.numBlack + " w: " + n.board.numWhite);
		tree.printTile(n.board.board, board1.size);
		System.out.println("end move");
		
		Vector<Tree.move> path = new Vector<Tree.move>();
		////System.out.println("test" + (tree.visitedNodes.size()));
		//path = tree.visitedNodes.get(tree.visitedNodes.size()-1).cur_path;
		path = n.cur_path;
		System.out.println("p: " + n.h);
		
		if(tree.isNull(n) || path.isEmpty() || player!=path.get(0).player)
		{
			System.out.println("AHHHHHHHHHHHHH2");
			if(player == 1)
			{
				newmove[0] = -1;
				newmove[1] = 0;				
			}
			else
			{
				newmove[0] = 0;
				newmove[1] = -1;
			}
			return;
		}
		/*if(player != path.get(0).player)
		{
			System.out.println("how many?");
			playerSwitch = true;
			return;
		}*/
		/*Tree.node p;
		if(n != null && n.parent != null)
		{
			p = n.parent;
			while(p != null || p.parent != null )//&& p.cur_path.containsAll(n.cur_path))
				p = p.parent;
			tree.printTile(p.board.board, p.board.size);
			board1.insertPiece(path.get(0)[0], path.get(0)[1], p.player);
		}
		else*/
			board1.insertPiece(path.get(0).x, path.get(0).y, path.get(0).player);
		/*if(board1.noMoreValidMoves(player) || path == null) //FIXME this will use its own method to also get all peices/moves
		{
			System.out.println("AHHHHHHHHHHHHHHHHHHHHHHHH" + path);
			if(player == 1)
			{
				newmove[0] = -1;
				newmove[1] = 0;				
			}
			else
			{
				newmove[0] = 0;
				newmove[1] = -1;
			}
			return;
		}*/
		
		for(int i = 0; i < path.size(); i++)
		{
			////System.out.println(i + " :: x: " + path.get(i)[0] + " y: " + path.get(i)[1] + "hue: " + n.h);
			
		}
		
		//Tree.node tmpN = tree.new node(board1, 0, 0, path);
		/*//System.out.println("s1");
		//int s1[][] = tree.applyOPstar(path.get(0)[0], path.get(0)[1], player, tmpN).board.board;
		tree.printTile(board1.board, board1.size);
		//System.out.println("end s1");*/
		
		//board1.insertPiece(path.get(0)[0], path.get(0)[1], player);
		newmove[0] = path.get(0).x;
		newmove[1] = path.get(0).y;
		//System.out.println("newmove " + newmove[0] + " " + newmove[1]);
		//path.remove(0);
	
	}
	
	public void makeBadMove(int boardsize, int[][] boardstate, int[] lastmove, Long maxtime, int[] newmove)
	{
		playerSwitch = false;
		//board1.board = boardstate;
		//board1.size = boardsize;
		if(lastmove[0] != -1 && lastmove[1] != -1){
			System.out.println("player is " + player);
			board1.insertPiece(lastmove[0], lastmove[1], -player);
		}
		
		Board tmpBoard = new Board(board1.size);
		tmpBoard.copy(board1);
		/*for(int i = 0; i < board1.size; i++)
			for(int j = 0; j < board1.size; j++)
				tmpBoard.board[i][j] = board1.board[i][j];*/
		
		/*System.out.println("move" + 1);
		tree.printTile(board1.board, board1.size);
		System.out.println("end move");*/
		
		Long s = System.currentTimeMillis();
		Tree.node n = tree.idastarbad(tmpBoard, 3, player, numMoves, maxtime);
		Long e = System.currentTimeMillis();
		System.out.println("HOW LONG - " + (e-s));
		numMoves++;
		
		System.out.println("move b: " + n.board.numBlack + " w: " + n.board.numWhite);
		tree.printTile(n.board.board, board1.size);
		System.out.println("end move");
		
		Vector<Tree.move> path = new Vector<Tree.move>();
		////System.out.println("test" + (tree.visitedNodes.size()));
		//path = tree.visitedNodes.get(tree.visitedNodes.size()-1).cur_path;
		path = n.cur_path;
		System.out.println("p: " + n.h);
		
		if(tree.isNull(n) || path.isEmpty() || player!=path.get(0).player)
		{
			System.out.println("AHHHHHHHHHHHHH2");
			if(player == 1)
			{
				newmove[0] = -1;
				newmove[1] = 0;				
			}
			else
			{
				newmove[0] = 0;
				newmove[1] = -1;
			}
			return;
		}
		/*if(player != path.get(0).player)
		{
			System.out.println("how many?");
			playerSwitch = true;
			return;
		}*/
		/*Tree.node p;
		if(n != null && n.parent != null)
		{
			p = n.parent;
			while(p != null || p.parent != null )//&& p.cur_path.containsAll(n.cur_path))
				p = p.parent;
			tree.printTile(p.board.board, p.board.size);
			board1.insertPiece(path.get(0)[0], path.get(0)[1], p.player);
		}
		else*/
			board1.insertPiece(path.get(0).x, path.get(0).y, path.get(0).player);
		/*if(board1.noMoreValidMoves(player) || path == null) //FIXME this will use its own method to also get all peices/moves
		{
			System.out.println("AHHHHHHHHHHHHHHHHHHHHHHHH" + path);
			if(player == 1)
			{
				newmove[0] = -1;
				newmove[1] = 0;				
			}
			else
			{
				newmove[0] = 0;
				newmove[1] = -1;
			}
			return;
		}*/
		
		for(int i = 0; i < path.size(); i++)
		{
			////System.out.println(i + " :: x: " + path.get(i)[0] + " y: " + path.get(i)[1] + "hue: " + n.h);
			
		}
		
		//Tree.node tmpN = tree.new node(board1, 0, 0, path);
		/*//System.out.println("s1");
		//int s1[][] = tree.applyOPstar(path.get(0)[0], path.get(0)[1], player, tmpN).board.board;
		tree.printTile(board1.board, board1.size);
		//System.out.println("end s1");*/
		
		//board1.insertPiece(path.get(0)[0], path.get(0)[1], player);
		newmove[0] = path.get(0).x;
		newmove[1] = path.get(0).y;
		//System.out.println("newmove " + newmove[0] + " " + newmove[1]);
		//path.remove(0);
	
	}
	
}

